﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyTetris
{
    /// <summary>
    /// 各类方块的基础
    /// </summary>
    public abstract class BaseBlock
    {

        protected List<Square> squares = new List<Square>();
        protected abstract void InitSquares();
        protected abstract void SetOutterPosition(int indexX, int indexY);

        public event Action ArriveBottomEvent;

        public BaseBlock(int indexX,int indexY)
        {
            InitSquares();
            SetOutterPosition(indexX, indexY);

            //设置各个小方块的旋转中心点
            foreach(var s in squares)
            {
                s.CenterIndexX = CenterIndexX;
                s.CenterIndexY = CenterIndexY;
            }
            
        }

        protected BaseBlock()
        { }

        public IEnumerable<Square> Squares
        {
            get
            {
                return squares;
            }
        }

        public abstract int CenterIndexX { set; get; }
        public abstract int CenterIndexY { set; get; }

        //判断小方块能否旋转
        public bool CanRotate()
        {
            bool tag = true;
            foreach(Square s in squares)
            {
                if(!s.CanRotate())
                {
                    tag = false;
                    break;
                }
            }
            return tag;
        }
        //小方块旋转的实现
        public void Rotate()
        {
            if (CanRotate())
            {
                foreach(var s in squares)
                {
                    s.Rotate();
                }
            }
        }

        public void Draw(Graphics g)
        {
            foreach(var s in squares)
            {
                s.Draw(g);
            }

        }

        //判断小方块能否下落
        public bool CanDown()
        {
            bool tag = true;
            foreach (Square s in squares)
            {
                if (!s.CanDown())
                {
                    tag = false;
                    break;
                }
            }
            return tag;
        }

        internal void Draw(object graphics)
        {
            throw new NotImplementedException();
        }

        //小方块下落的实现
        public void MoveDown()
        {
            if (CanDown())
            {
                foreach(var s in squares)
                {
                    s.MoveDown();
                }
            }
            else
            {
                foreach(var s in squares)
                {
                    GameEngine.Instance.matrix[s.IndexTop, s.IndexLeft] = 1;
                }
                TriggerArriveBottomEvent();
            }
        }

        //判断小方块能否向左移动
        public bool CanLeft()
        {
            bool tag = true;
            foreach (Square s in squares)
            {
                if (!s.CanLeft())
                {
                    tag = false;
                    break;
                }
            }
            return tag;
        }
        //小方块向左移动的实现
        public void MoveLeft()
        {
            if (CanLeft())
            {
                foreach (var s in squares)
                {
                    s.MoveLeft();
                }
            }
            else
            {
                foreach (var s in squares)
                {
                    GameEngine.Instance.matrix[s.IndexTop, s.IndexLeft] = 1;
                }
            }
        }

        //判断小方块能否向右移动
        public bool CanRight()
        {
            bool tag = true;
            foreach (Square s in squares)
            {
                if (!s.CanRight())
                {
                    tag = false;
                    break;
                }
            }
            return tag;
        }
        //小方块向右移动的实现
        public void MoveRight()
        {
            if (CanRight())
            {
                foreach (var s in squares)
                {
                    s.MoveRight();
                }
            }
            else
            {
                foreach (var s in squares)
                {
                    GameEngine.Instance.matrix[s.IndexTop, s.IndexLeft] = 1;
                }
            }
        }

        public void TriggerArriveBottomEvent()
        {
            if (ArriveBottomEvent != null)
            {
                ArriveBottomEvent();
            }
        }
    }
}
